package myzzyl.domain.mapper;

import myzzyl.domain.pojo.JwtUser;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.Map;

@Mapper
public interface JwtUserMapper {
    JwtUserMapper INSTANCE = Mappers.getMapper(JwtUserMapper.class);


    @Mapping(source = "id", target = "id", qualifiedByName = "objectToLong")
    @Mapping(source = "name", target = "name", qualifiedByName = "objectToString")
    @Mapping(source = "openId", target = "openId", qualifiedByName = "objectToString")
    @Mapping(source = "avatar", target = "avatar", qualifiedByName = "objectToString")
    JwtUser mapFromClaims(Map<String, Object> claims);

    @Named("objectToLong")
    default Long objectToLong(Object value) {
        if (value instanceof Long) {
            return (Long) value;
        } else if (value instanceof String) {
            return Long.parseLong((String) value);
        } else {
            throw new IllegalArgumentException("Unsupported type for mapping: " + value.getClass());
        }
    }

    @Named("objectToString")
    default String objectToString(Object value) {
        if (value instanceof String) {
            return (String) value;
        } else {
            throw new IllegalArgumentException("Unsupported type for mapping: " + value.getClass());
        }
    }
}
